<?php

declare(strict_types=1);

namespace App\Service\Logic;

use App\Service\Business\CustomModuleDataValueBusiness;
use App\Service\Business\CustomModuleFieldBusiness;
use App\Validator\CommonValidator;
use App\Validator\CustomModuleDataValueValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块数据值-逻辑类
 */
class CustomModuleDataValueLogic extends BaseLogic
{
        #[Inject]
        protected CustomModuleDataValueBusiness $CustomModuleDataValueBusiness;

        #[Inject]
        protected CustomModuleDataValueValidator $CustomModuleDataValueValidator;

        #[Inject]
        protected CommonValidator $CommonValidator;

        #[Inject]
        protected CustomModuleFieldBusiness $CustomModuleFieldBusiness;

        /**
         * 分页
         * @return array
         * @throws Exception
         */
        public function paginate(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['page', 'limit']);
                return $this->CustomModuleDataValueBusiness->paginate();
        }

        /**
         * 设置
         * @param mixed $customModuleDataId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function set(mixed $customModuleDataId): array
        {
                $this->CustomModuleDataValueValidator->validate($this->request->all(), ['customModuleId']);
                $arrCustomModuleField = $this->CustomModuleFieldBusiness->pluck([
                        'wheres' => ['customModuleId' => $this->request->input('customModuleId')],
                        'column' => 'fieldName',
                ])['data'];
                $arrInserts = [];
                foreach ($arrCustomModuleField as $v) {
                        $arrInserts[] = [
                                'customModuleDataValueId' => snowflakeId(),
                                'customModuleDataId' => $customModuleDataId,
                                'customModuleId' => $this->request->input('customModuleId'),
                                'fieldName' => $v,
                                'fieldValue' => json_encode($this->request->input($v), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
                        ];
                }
                return $this->CustomModuleDataValueBusiness->set($customModuleDataId, $arrInserts);
        }

        /**
         * 关联性删除
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function relevanceDelete(): array
        {
                $this->CustomModuleDataValueValidator->validate($this->request->all(), [], ['customModuleId', 'customModuleDataId']);
                return $this->CustomModuleDataValueBusiness->relevanceDelete(
                        $this->request->input('customModuleId'), $this->request->input('customModuleDataId'));
        }
}